package com.yc.booktable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.entity.Food;
import com.yc.booktable.mapper.FoodMapper;
import com.yc.booktable.service.IFoodService;
import com.yc.booktable.vo.Result;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author zbh
 * @since 2022-06-06
 */
@Controller
@RequestMapping("/menu")
public class FoodController {
    @Resource
    private IFoodService foodService;
    @Resource
    private FoodMapper foodMapper;


    @GetMapping("menus")
    @ResponseBody
    public Result menus( Model model){
        List<Food> list = foodService.list();
        return new Result(1,"获取成功",list);
    }
    @GetMapping
    public String menu(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",user);
        return "menu";
    }

    @GetMapping("menuftype/{ftype}")
    @ResponseBody
    public Result menuftype(@PathVariable("ftype") String ftype, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        QueryWrapper<Food> queryWrapper = new QueryWrapper<Food>();
        queryWrapper.eq("ftype",ftype);
        List<Food> list = foodService.list(queryWrapper);
        return new Result(1,"获取成功",list);
    }

    @GetMapping("special")
    @ResponseBody
    public Result special(){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("ftype","特色");
        List list = foodService.list(wrapper);
        return new Result(1,"查询成功",list);
    }
    @GetMapping("special/{fid}")
    @ResponseBody
    public Result special(@PathVariable("fid") String fid){
        QueryWrapper wrapper = new QueryWrapper();
        System.out.println(fid);
        Integer id =  Integer.parseInt(fid);
        wrapper.eq("fid",id);
        Food food = foodService.getOne(wrapper);
        return new Result(1,"查询成功",food);
    }
}

